import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../themes/multiple_themes_mode/app_default_style.dart';
import '../../logger.dart';
import '../smart_refresher_by_flow.dart';
import '../smart_refresher_by_grid.dart';
import '../smart_refresher_by_list.dart';
import 'base_refresher_controller.dart';

enum ListType { list, flow, grid }

class BaseRefresherView extends StatefulWidget {
  const BaseRefresherView({
    super.key,
    this.header,
    required this.item,
    required this.apiFn,
    this.needAnimation = true,
    this.refreshFooterColor,
    this.padding,
    this.crossAxisSpacing = 0,
    this.mainAxisSpacing = 0,
    this.useFraming = true,
    this.isFlex = false,
    this.listType = ListType.list,
    this.crossAxisCount = 1,
  });

  final Widget Function(dynamic data) item;
  final Function(int pageIndex) apiFn;
  final ListType? listType;
  final int crossAxisCount;

  final Widget? header;

  //是否需要下拉动画
  final bool needAnimation;
  final Color? refreshFooterColor;
  //间距的问题
  final EdgeInsetsGeometry? padding;

  //流式布局Grid布局 需要的 顶部广告是否 全部占用 默认全部占用
  final bool isFlex;

  //纵向间距
  final double crossAxisSpacing;

  //横向间距
  final double mainAxisSpacing;

  //是否需要 分帧技术
  final bool useFraming;

  @override
  State<BaseRefresherView> createState() => _BaseRefresherViewState();
}

class _BaseRefresherViewState extends State<BaseRefresherView> {
  late BaseRefresherController ctrl;
  final String tag = DateTime.now().microsecondsSinceEpoch.toString();
  late RefreshController refreshCtrl =
      RefreshController(initialRefresh: widget.needAnimation);
  @override
  void initState() {
    super.initState();

    ///这样做的好处是预防Get对视屏不进行销毁
    ctrl = Get.put(
      BaseRefresherController(
        apiFn: widget.apiFn,
      ),
      tag: tag,
    );

    //如果不需要动画 则主动加载
    if (!widget.needAnimation) {
      ctrl.onRefresh(refreshCtrl: refreshCtrl);
    }
  }

  @override
  void dispose() {
    CoreKitLogger().d('BaseRefresherController dispose $tag start');
    Get.delete<BaseRefresherController>(tag: tag);

    super.dispose();
    CoreKitLogger().d('BaseRefresherController dispose $tag end');
  }

  @override
  Widget build(BuildContext context) {
    Widget smartRefresher = Container();

    switch (widget.listType) {
      case ListType.list:
        smartRefresher = Obx(
          () => SmartRefresherByList(
            padding: widget.padding ?? EdgeInsets.all(AppStyle.padding),
            useFraming: widget.useFraming,
            itmList: widget.header != null
                ? ctrl.dataList.length + 1
                : ctrl.dataList.length,
            controller: refreshCtrl,
            onRefresh: () => ctrl.onRefresh(refreshCtrl: refreshCtrl),
            onLoading: () => ctrl.onLoading(refreshCtrl: refreshCtrl),
            child: (context, index) {
              var i = index;
              if (widget.header != null) {
                i--;
              }
              if (widget.header != null && index == 0) {
                return widget.header;
              }

              final dynamic data = ctrl.dataList[i];
              return widget.item(data);
            },
          ),
        );
        break;
      case ListType.grid:
        smartRefresher = Obx(
          () => SmartRefresherByGrid(
            useFraming: widget.useFraming,
            header: widget.header,
            refreshFooterColor: widget.refreshFooterColor,
            crossAxisCount: widget.crossAxisCount,
            padding: widget.padding ?? EdgeInsets.all(AppStyle.padding),
            itmList: ctrl.dataList.isEmpty
                ? 0
                : ctrl.dataList.length + widget.crossAxisCount,
            controller: refreshCtrl,
            crossAxisSpacing: widget.crossAxisSpacing,
            mainAxisSpacing: widget.mainAxisSpacing,
            onRefresh: () => ctrl.onRefresh(refreshCtrl: refreshCtrl),
            onLoading: () => ctrl.onLoading(refreshCtrl: refreshCtrl),
            child: (context, index) {
              if (index < widget.crossAxisCount) {
                return Container();
              }
              final dynamic data = ctrl.dataList[index - widget.crossAxisCount];
              return widget.item(data);
            },
          ),
        );
        break;
      case ListType.flow:
        smartRefresher = Obx(
          () => SmartRefresherByFlow(
            useFraming: widget.useFraming,
            header: widget.header,
            crossAxisCount: widget.crossAxisCount,
            padding: widget.padding ?? EdgeInsets.all(AppStyle.padding),
            itmList: ctrl.dataList.length,
            controller: refreshCtrl,
            onRefresh: () => ctrl.onRefresh(refreshCtrl: refreshCtrl),
            onLoading: () => ctrl.onLoading(refreshCtrl: refreshCtrl),
            child: (context, index) {
              final dynamic data = ctrl.dataList[index];
              return widget.item(data);
            },
          ),
        );
        break;
      case null:
        break;
    }

    return SizedBox(
      width: Get.width,
      height: Get.height,
      child: smartRefresher,
    );
  }
}
